/*
 * Created by JFormDesigner on Tue May 06 13:28:10 CST 2025
 */

package com.soft.bams.client;

import com.soft.bams.entity.Account;
import com.soft.bams.exception.RegisterException;
import com.soft.bams.exception.TypeException;
import com.soft.bams.service.Bank;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 21603
 */
public class RegisterPanel extends JPanel {
    ATMClient atmClient;

    public RegisterPanel(ATMClient atmClient) {
        this.atmClient = atmClient;
        initComponents();
    }

    private void submit(ActionEvent e) {
        try {
            int selectedIndex = typeComboBox.getSelectedIndex();
            String name = nameTextField.getText();
            String password = new String(passwordField.getPassword());
            String repassword = new String(repasswordField.getPassword());
            String idCard = idCardTextField.getText();
            String email = emailTextField.getText();

            String command = String.format("register#%s#%s#%s#%s#%s#%d",
                    password, repassword, name, idCard, email, selectedIndex);

            Object response = atmClient.sendCommand(command);

            if (response instanceof Account) {
                atmClient.account = (Account) response;
                atmClient.loginPanel.initLogin();
                CardLayout cardLayout = (CardLayout) getParent().getLayout();
                cardLayout.show(getParent(), "login");
                clearRegister();
            } else if (response instanceof String) {
                JOptionPane.showMessageDialog(getParent(), response);
            } else {
                JOptionPane.showMessageDialog(getParent(), "注册失败: 未知错误");
            }
        } catch (Exception exception) {
            JOptionPane.showMessageDialog(getParent(), "注册异常: " + exception.getMessage());
        }
    }


    private void back(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(), "main");
    }

    private void clearRegister() {
        typeComboBox.setSelectedIndex(0);
        nameTextField.setText("");
        passwordField.setText("");
        repasswordField.setText("");
        idCardTextField.setText("");
        emailTextField.setText("");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        typeLabel = new JLabel();
        nameLabel = new JLabel();
        passwordLabel = new JLabel();
        repasswordLabel = new JLabel();
        idCardLabel = new JLabel();
        emailLabel = new JLabel();
        typeComboBox = new JComboBox<>();
        nameTextField = new JTextField();
        passwordField = new JPasswordField();
        repasswordField = new JPasswordField();
        idCardTextField = new JTextField();
        emailTextField = new JTextField();
        submitButton = new JButton();
        backButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- typeLabel ----
        typeLabel.setText("\u8d26\u6237\u7c7b\u578b\uff1a");
        add(typeLabel);
        typeLabel.setBounds(new Rectangle(new Point(90, 25), typeLabel.getPreferredSize()));

        //---- nameLabel ----
        nameLabel.setText("\u7528 \u6237 \u540d\uff1a");
        add(nameLabel);
        nameLabel.setBounds(new Rectangle(new Point(95, 65), nameLabel.getPreferredSize()));

        //---- passwordLabel ----
        passwordLabel.setText("\u5bc6    \u7801\uff1a");
        add(passwordLabel);
        passwordLabel.setBounds(new Rectangle(new Point(100, 105), passwordLabel.getPreferredSize()));

        //---- repasswordLabel ----
        repasswordLabel.setText("\u786e\u8ba4\u5bc6\u7801\uff1a");
        add(repasswordLabel);
        repasswordLabel.setBounds(new Rectangle(new Point(90, 145), repasswordLabel.getPreferredSize()));

        //---- idCardLabel ----
        idCardLabel.setText("\u8eab\u4efd\u8bc1\u53f7\uff1a");
        add(idCardLabel);
        idCardLabel.setBounds(new Rectangle(new Point(90, 185), idCardLabel.getPreferredSize()));

        //---- emailLabel ----
        emailLabel.setText("E-Mail:");
        add(emailLabel);
        emailLabel.setBounds(new Rectangle(new Point(100, 230), emailLabel.getPreferredSize()));

        //---- typeComboBox ----
        typeComboBox.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u50a8\u84c4\u8d26\u6237",
            "\u4fe1\u7528\u8d26\u6237",
            "\u53ef\u8d37\u6b3e\u7684\u50a8\u84c4\u8d26\u6237",
            "\u53ef\u8d37\u6b3e\u7684\u4fe1\u7528\u8d26\u6237"
        }));
        add(typeComboBox);
        typeComboBox.setBounds(200, 20, 160, typeComboBox.getPreferredSize().height);
        add(nameTextField);
        nameTextField.setBounds(200, 60, 160, nameTextField.getPreferredSize().height);
        add(passwordField);
        passwordField.setBounds(200, 100, 160, passwordField.getPreferredSize().height);
        add(repasswordField);
        repasswordField.setBounds(200, 140, 160, repasswordField.getPreferredSize().height);
        add(idCardTextField);
        idCardTextField.setBounds(200, 180, 160, idCardTextField.getPreferredSize().height);
        add(emailTextField);
        emailTextField.setBounds(200, 225, 160, emailTextField.getPreferredSize().height);

        //---- submitButton ----
        submitButton.setText("\u63d0\u4ea4");
        submitButton.addActionListener(e -> submit(e));
        add(submitButton);
        submitButton.setBounds(new Rectangle(new Point(125, 285), submitButton.getPreferredSize()));

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(new Rectangle(new Point(235, 285), backButton.getPreferredSize()));

        setPreferredSize(new Dimension(445, 370));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel typeLabel;
    private JLabel nameLabel;
    private JLabel passwordLabel;
    private JLabel repasswordLabel;
    private JLabel idCardLabel;
    private JLabel emailLabel;
    private JComboBox<String> typeComboBox;
    private JTextField nameTextField;
    private JPasswordField passwordField;
    private JPasswordField repasswordField;
    private JTextField idCardTextField;
    private JTextField emailTextField;
    private JButton submitButton;
    private JButton backButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
